package com.pingancar.creditmanage.dao;

import com.pingancar.creditmanage.pojo.OrderitemPojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.pojo.UserPojo;
import com.pingancar.creditmanage.util.myenum.OrderListField;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 *Created by system on Tue Sep 09 11:17:13 CST 2014
 */
@Repository
public class OrderitemDaoImpl extends BaseDAO<OrderitemPojo> {

    @Override
    public Integer save(OrderitemPojo orderlist){
        return (Integer)super.save(orderlist);
	}

    /**
     * 通过用户分页加载订单列表
     * @param user
     * @return
     */
    public List<OrderitemPojo> getOrderlistByUser(UserPojo user, int offset, int length, int status) {
        String hql = "from OrderitemPojo oi where oi.user.id = :uid and oi.status = :status" + getOrderHQL("oi.id", DESC);
        return getSession().createQuery(hql)
                .setInteger("uid", user.getId()).setInteger("status", status)
                .setFirstResult(offset).setMaxResults(length)
                .list();
    }

    public List<OrderitemPojo> getOrderlistByShop(ShopinfoPojo shopinfoPojo, Integer offset, Integer length, Integer[] status) {
        String str = "(";
        for (int i=0;i<status.length;i++) {
            if (i != 0)
                str += ",";
            str += status[i];
        }
        str += ") ";
        String hql = "from OrderitemPojo oi where oi.paservice.shopinfo.id = :sid and oi.status in " + str + getOrderHQL("oi.id", DESC);
        return getSession().createQuery(hql)
                .setInteger("sid", shopinfoPojo.getId())
                .setFirstResult(offset).setMaxResults(length)
                .list();
    }
}